/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.icanci.loopstack.damc.admin.dal.mongo.mongo;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import cn.icanci.loopstack.damc.admin.dal.common.PageList;
import cn.icanci.loopstack.damc.admin.dal.mongo.daointerface.DisposableScriptDAO;
import cn.icanci.loopstack.damc.admin.dal.mongo.dateobject.DisposableScriptDO;

/**
 * @author icanci(1205068)
 * @version Id: MongoDisposableScriptDAO, v 0.1 2025/5/26 20:46 icanci Exp $
 */
@Service("disposableScriptDAO")
public class MongoDisposableScriptDAO extends AbstractBaseDAO<DisposableScriptDO> implements DisposableScriptDAO {
    @Override
    public void insert(DisposableScriptDO disposableScript) {
        super.insert(disposableScript);
        mongoTemplate.insert(disposableScript, COLLECTION_NAME);
    }

    @Override
    public void update(DisposableScriptDO disposableScript) {
        super.update(disposableScript);
        mongoTemplate.save(disposableScript, COLLECTION_NAME);
    }

    /**
     * 查询文档所有记录
     *
     * @return 返回查询的结果
     */
    @Override
    public List<DisposableScriptDO> queryAll() {
        Criteria criteria = Criteria.where(ClassScriptColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.find(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    /**
     * 查询文档所有记录
     *
     * @param disposableScriptDO 请求参数
     * @param pageNum            pageNum
     * @param pageSize           pageSize
     * @return 返回查询的结果
     */
    @Override
    public PageList<DisposableScriptDO> pageQuery(DisposableScriptDO disposableScriptDO, int pageNum, int pageSize) {
        Criteria criteria = Criteria.where(ClassScriptColumn.ENV).is(DEFAULT_ENV);
        // 查询参数
        if (StringUtils.isNotBlank(disposableScriptDO.getAppCode())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(ClassScriptColumn.APP_CODE).is(disposableScriptDO.getAppCode());
        }

        if (StringUtils.isNotBlank(disposableScriptDO.getDesc())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(ClassScriptColumn.DESC).regex("^.*" + disposableScriptDO.getDesc() + ".*$", "i");
        }

        if (StringUtils.isNotBlank(disposableScriptDO.getRunnerType())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(ClassScriptColumn.RUNNER_TYPE).is(disposableScriptDO.getRunnerType());
        }

        if (StringUtils.isNotBlank(disposableScriptDO.getScript())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(ClassScriptColumn.SCRIPT).regex("^.*" + disposableScriptDO.getScript() + ".*$", "i");
        }

        Query query = new Query(criteria);
        query.with(Sort.by(Sort.Direction.DESC, ClassScriptColumn.CREATE_TIME));
        return pageQuery(query, COLLECTION_CLASS, pageSize, pageNum, COLLECTION_NAME);
    }

    /**
     * 根据 _id 查询一条信息
     *
     * @param _id _id
     * @return 返回查询的结果
     */
    @Override
    public DisposableScriptDO queryOneById(String _id) {
        Criteria criteria = Criteria.where(ClassScriptColumn._ID).is(_id);
        criteria.and(ClassScriptColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public Class<? extends BaseColumn> getBaseColumn() {
        return ClassScriptColumn.class;
    }

    @Override
    public void deleteById(String id) {
        Criteria criteria = Criteria.where(ClassScriptColumn._ID).is(id);
        criteria.and(ClassScriptColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        mongoTemplate.remove(query, COLLECTION_CLASS, COLLECTION_NAME);
    }
}